unit Chromium4D.Extension.Buffer;

interface

  uses System.Classes, System.SysUtils, ceflib
    , Chromium4D.Core
    , Chromium4D.Render.Bundle
    , Chromium4D.Render.Environment
    , Chromium4D.Extension
    ;

type

  TSyExtBufferPrototype = class(TSyV8ExtHandler)
  private
    procedure DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
  protected
    function AsciiSlice(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Base64Slice(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function BinarySlice(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function HexSlice(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Ucs2Slice(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Utf8Slice(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;

    function AsciiWrite(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Base64Write(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function BinaryWrite(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function HexWrite(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Ucs2Write(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Utf8Write(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;

    function JCopy(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
  public
    class procedure ObjectBuilder(process: TSyChromiumProcess; env: TSyRenderEnvironment; context: ICefv8Context; target: ICefv8Value); virtual;
  end;

  TSyExtBindingObject = class(TSyV8ExtHandler)
  private
    procedure DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
  protected
    function CreateBuffer(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function SubArray(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function SetAt(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function GetAt(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
  public
    class procedure ObjectBuilder(process: TSyChromiumProcess; env: TSyRenderEnvironment; context: ICefv8Context; target: ICefv8Value); virtual;
  end;

  TSyExtBuffer = class(TSyV8ExtHandler)
  private
    procedure DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
  protected
    function SetupBufferJS(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;

    function CreateFromString(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;

    function ByteLengthUtf8(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Compare(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function Fill(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function IndexOfBuffer(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function IndexOfNumber(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function IndexOfString(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;

    function ReadDoubleBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function ReadDoubleLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function ReadFloatBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function ReadFloatLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;

    function WriteDoubleBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function WriteDoubleLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function WriteFloatBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
    function WriteFloatLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
  public
    class function ObjectFactory(process: TSyChromiumProcess; env: TSyRenderEnvironment; context: ICefv8Context): ICefv8Value; override;
  end;

  TSyExtBindingObjectFlagsAccessor = class(TSyV8ExtPropertyAccessor)
  public
    function Get(const name: ustring; const obj: ICefv8Value; out value: ICefv8Value;
      const exception: ustring): Boolean; override;
    function Put(const name: ustring; const obj, value: ICefv8Value; const exception: ustring): Boolean; override;
  end;

  TSyInternalBuffer = class
  public
    constructor Create(AEnv: TSyRenderEnvironment; ASize: Integer); virtual;
    destructor Destroy; override;

    function Wrap: ICefv8Value;
  end;

implementation

{ TSyExtNatives }

function TSyExtBuffer.ByteLengthUtf8(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.Compare(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.CreateFromString(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

procedure TSyExtBuffer.DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
begin
  target.SetValueByKey('kMaxLength', TCefv8ValueRef.NewInt(MaxInt), V8_PROPERTY_ATTRIBUTE_NONE);
  target.SetValueByKey('kStringMaxLength', TCefv8ValueRef.NewInt(MaxInt), V8_PROPERTY_ATTRIBUTE_NONE);

  DefineObjectFunction(target, 'setupBufferJS'   , SetupBufferJS);

  DefineObjectFunction(target, 'createFromString', CreateFromString);

  DefineObjectFunction(target, 'byteLengthUtf8'  , ByteLengthUtf8);
  DefineObjectFunction(target, 'compare'         , Compare);
  DefineObjectFunction(target, 'fill'            , Fill);
  DefineObjectFunction(target, 'indexOfBuffer'   , IndexOfBuffer);
  DefineObjectFunction(target, 'indexOfNumber'   , IndexOfNumber);
  DefineObjectFunction(target, 'indexOfString'   , IndexOfString);

  DefineObjectFunction(target, 'readDoubleBE'    , ReadDoubleBE);
  DefineObjectFunction(target, 'readDoubleLE'    , ReadDoubleLE);
  DefineObjectFunction(target, 'readFloatBE'     , ReadFloatBE);
  DefineObjectFunction(target, 'readFloatLE'     , ReadFloatLE);

  DefineObjectFunction(target, 'writeDoubleBE'   , WriteDoubleBE);
  DefineObjectFunction(target, 'writeDoubleLE'   , WriteDoubleLE);
  DefineObjectFunction(target, 'writeFloatBE'    , WriteFloatBE);
  DefineObjectFunction(target, 'writeFloatLE'    , WriteFloatLE);
end;

function TSyExtBuffer.Fill(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.IndexOfBuffer(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.IndexOfNumber(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.IndexOfString(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

class function TSyExtBuffer.ObjectFactory(process: TSyChromiumProcess; env: TSyRenderEnvironment; context: ICefv8Context): ICefv8Value;
var
  ObjectFactory: TSyExtBuffer;
begin
  ObjectFactory := TSyExtBuffer.Create;
  ObjectFactory.FEnv := env;
  Result := TCefv8ValueRef.NewObject(nil);
  ObjectFactory.DefineJavascript(env, context, Result);
end;

function TSyExtBuffer.ReadDoubleBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.ReadDoubleLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.ReadFloatBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.ReadFloatLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.SetupBufferJS(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
var
  context: ICefv8Context;
begin
  context := TCefv8ContextRef.Current;

  if (Length(arguments) > 0)and (arguments[0].IsObject) then
  begin
    FEnv.EnvObject.Setptype_buffer(arguments[0]);
    TSyExtBufferPrototype.ObjectBuilder(nil, FEnv, context, arguments[0]);
  end;

  if (Length(arguments) > 1)and (arguments[1].IsObject) then
  begin
    FEnv.EnvObject.Setptype_buffer(arguments[0]);
    TSyExtBindingObject.ObjectBuilder(nil, FEnv, context, arguments[1]);
  end;
end;

function TSyExtBuffer.WriteDoubleBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.WriteDoubleLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.WriteFloatBE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBuffer.WriteFloatLE(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

{ TSyExtBindingObject }

function TSyExtBindingObject.CreateBuffer(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin
  if Length(arguments) = 1 then
  begin
    retval := TSyInternalBuffer.Create(FEnv, arguments[0].GetIntValue).Wrap;
  end;
end;

procedure TSyExtBindingObject.DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
begin
  // functions
  DefineObjectFunction(target, 'createBuffer', CreateBuffer);
  DefineObjectFunction(target, 'subarray', SubArray);
  DefineObjectFunction(target, 'setAt', SetAt);
  DefineObjectFunction(target, 'getAt', GetAt);
  // accessors
  DefineObjectAccessor(target, 'flags', TSyExtBindingObjectFlagsAccessor.Create);
end;

function TSyExtBindingObject.GetAt(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

class procedure TSyExtBindingObject.ObjectBuilder(process: TSyChromiumProcess; env: TSyRenderEnvironment;
  context: ICefv8Context; target: ICefv8Value);
var
  BindingObject: TSyExtBindingObject;
begin
  BindingObject := TSyExtBindingObject.Create;
  BindingObject.FEnv := env;
  BindingObject.DefineJavascript(env, context, target);
end;

function TSyExtBindingObject.SetAt(const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value;
  var exception: ustring): Boolean;
begin

end;

function TSyExtBindingObject.SubArray(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

{ TSyExtBufferPrototype }

function TSyExtBufferPrototype.AsciiSlice(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.AsciiWrite(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.Base64Slice(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.Base64Write(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.BinarySlice(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.BinaryWrite(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

procedure TSyExtBufferPrototype.DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
begin
  DefineObjectFunction(target, 'asciiSlice',  AsciiSlice);
  DefineObjectFunction(target, 'base64Slice', Base64Slice);
  DefineObjectFunction(target, 'binarySlice', BinarySlice);
  DefineObjectFunction(target, 'hexSlice',    HexSlice);
  DefineObjectFunction(target, 'ucs2Slice',   Ucs2Slice);
  DefineObjectFunction(target, 'utf8Slice',   Utf8Slice);

  DefineObjectFunction(target, 'asciiWrite',  AsciiWrite);
  DefineObjectFunction(target, 'base64Write', Base64Write);
  DefineObjectFunction(target, 'binaryWrite', BinaryWrite);
  DefineObjectFunction(target, 'hexWrite',    HexWrite);
  DefineObjectFunction(target, 'ucs2Write',   Ucs2Write);
  DefineObjectFunction(target, 'utf8Write',   Utf8Write);

  DefineObjectFunction(target, 'copy',        JCopy);
end;

function TSyExtBufferPrototype.HexSlice(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.HexWrite(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.JCopy(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

class procedure TSyExtBufferPrototype.ObjectBuilder(process: TSyChromiumProcess; env: TSyRenderEnvironment;
  context: ICefv8Context; target: ICefv8Value);
var
  BufferPrototype: TSyExtBufferPrototype;
begin
  BufferPrototype := TSyExtBufferPrototype.Create;
  BufferPrototype.FEnv := env;
  BufferPrototype.DefineJavascript(env, context, target);
end;

function TSyExtBufferPrototype.Ucs2Slice(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.Ucs2Write(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.Utf8Slice(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

function TSyExtBufferPrototype.Utf8Write(const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

{ TSyExtBindingObjectFlagsAccessor }

function TSyExtBindingObjectFlagsAccessor.Get(const name: ustring; const obj: ICefv8Value; out value: ICefv8Value;
  const exception: ustring): Boolean;
begin

end;

function TSyExtBindingObjectFlagsAccessor.Put(const name: ustring; const obj, value: ICefv8Value;
  const exception: ustring): Boolean;
begin

end;

{ TSyInternalBuffer }

constructor TSyInternalBuffer.Create(AEnv: TSyRenderEnvironment; ASize: Integer);
begin

end;

destructor TSyInternalBuffer.Destroy;
begin

  inherited;
end;

function TSyInternalBuffer.Wrap: ICefv8Value;
begin

end;

end.
